package com.chen.project.white_jotter.controller;

import com.chen.project.white_jotter.pojo.Book;
import com.chen.project.white_jotter.service.LibraryService;
import com.chen.project.white_jotter.utils.RedisUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/library/")
public class LibraryController {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    LibraryService libraryService;

    // 获取所有图书
    @GetMapping("books")
    public List<Book> getAllBooks() {
        return libraryService.getAllBooks();
    }

    // 获取一个分类的图书
    @GetMapping("books/category/{cid}")
    public List<Book> getCategoryBooks(@PathVariable("cid") Integer cid) {
        return libraryService.getBooksByCategoryId(cid);
    }

    // mysql搜索图书
    @GetMapping("books/search")
    public List<Book> searchBooks(@RequestParam("keyword") String keyword) {
        if ("".equals(keyword)) {
            return libraryService.getAllBooks();
        }
        return libraryService.searchBooks(keyword);
    }
}
